<template>
  <div class="app-container">
    <div class="header">转赠设置</div>
    <el-form ref="transfer" :model="transfer" :rules="transferRules" label-width="80px">

      <el-form-item label="开始时间" prop="starttime">
        <el-time-picker
          value-format='HH:mm:ss'
          v-model="transfer.starttime"
          :picker-options="{
            selectableRange: '00:00:00 - 23:59:59'
           }"
          placeholder="选择开始时间">
        </el-time-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endtime">
        <el-time-picker
          v-model="transfer.endtime"
          value-format='HH:mm:ss'
          :picker-options="{
            selectableRange: '00:00:00 - 23:59:59'
           }"
          placeholder="选择结束时间">
        </el-time-picker>
      </el-form-item>
      <el-form-item label="起始数量" prop="startnumber">
        <el-input v-model="transfer.startnumber" placeholder="请输入上限数量"></el-input>
      </el-form-item>
      <el-form-item label="上限数量" prop="endnumber">
        <el-input v-model="transfer.endnumber" placeholder="请输入上限数量"></el-input>
      </el-form-item>
      <el-form-item label="转赠设置">
        <el-radio-group v-model="transfer.status">
          <el-radio :label="0">开启</el-radio>
          <el-radio :label="1">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="	手续费" prop="sxf">
        <el-input v-model="transfer.sxf" placeholder="请输入手续费"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="transferSubmit">保存</el-button>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <div class="header">登录设置</div>
    <el-form ref="login" :model="login" label-width="80px">
      <el-form-item label="设备验证">
        <el-radio-group v-model="login.status">
          <el-radio :label="0">开启</el-radio>
          <el-radio :label="1">关闭</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="loginSubmit">保存</el-button>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <div class="header">交易市场</div>
    <el-form ref="market" :model="market" :rules="marketRules" label-width="80px">
      <el-form-item label="抽成比率" prop="bilv">
        <el-input v-model="market.bilv" placeholder="请输入抽成比率"></el-input>
      </el-form-item>
      <el-form-item label="撤单时效" prop="orderTime">
        <el-input v-model="market.orderTime" placeholder="请输入撤单时效"></el-input>
      </el-form-item>
      <el-form-item label="单价上限" prop="endAmout">
        <el-input v-model="market.endAmout" placeholder="请输入上限数值"></el-input>
      </el-form-item>

      <el-form-item label="单价下限" prop="startAmout">
        <el-input v-model="market.startAmout" placeholder="请输入下限数值"></el-input>
      </el-form-item>
      <el-form-item label="上限数量" prop="endNumber">
        <el-input v-model="market.endNumber" placeholder="请输入上限数量"></el-input>
      </el-form-item>

      <el-form-item label="下限数量" prop="startNumber">
        <el-input v-model="market.startNumber" placeholder="请输入下限数量"></el-input>
      </el-form-item>
      <el-form-item label="撤单单位">
        <el-radio-group v-model="market.type">
          <el-radio :label="0">小时</el-radio>
          <el-radio :label="1">分钟</el-radio>
          <el-radio :label="2">秒</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <div class="header">分成设置</div>
    <div v-for="(item,index) in divideInto" :key="item.id">
      <el-form  label-width="80px">
        <el-form-item label="分成比率" >
          <el-input v-model="item.bilv" placeholder="请输入抽成比率"></el-input>
        </el-form-item>

        <el-form-item label="类型">
          <el-radio-group v-model="item.type">
            <el-radio :disabled="true" :label="0">直推</el-radio>
            <el-radio :disabled="true" :label="1">间推</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="divideIntoSubmit(item)">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider></el-divider>
    <div class="header">开启宝箱能量获得概率配置（概率合计相加需等于1）</div>
    <div v-for="(item,index) in EnergyList" :key="item.id">
      <el-form  label-width="120px" :inline="true" >
        <el-form-item label="获得能量概率" >
          <el-input v-model="item.prob" placeholder="请输入获得概率"></el-input>
        </el-form-item>
        <el-form-item label="开始数值" >
          <el-input v-model="item.startNumber" placeholder="请输入开始数值"></el-input>
        </el-form-item>
        <el-form-item label="结束数值" >
          <el-input v-model="item.endNumber" placeholder="请输入结束数值"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div style="margin-left: 70px">
      <el-button type="primary" @click="EnergyListSubmit()">保存</el-button>
    </div>
    <el-divider></el-divider>
    <div class="header">开启宝箱材料获得概率配置（概率合计相加需等于1）</div>
    <div v-for="(item,index) in StatisticalList" :key="item.id">
      <el-form  label-width="120px" :inline="true" >
        <el-form-item label="材料名称" >
          <el-input :disabled="true" v-model="item.materialName" ></el-input>
        </el-form-item>
        <el-form-item label="获得概率" >
          <el-input v-model="item.prob" placeholder="请输入获得概率"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div style="margin-left: 70px">
      <el-button type="primary" @click="StatisticalListoSubmit()">保存</el-button>
    </div>
    <el-divider></el-divider>
    <div class="header">靓号手续费设置</div>
    <div v-for="(item,index) in fancyNumber" :key="item.id">
      <el-form  label-width="120px" :inline="true" >
        <el-form-item :label="'靓号'+(item.extent)+'位手续费'" >
          <el-input v-model="item.bilv" placeholder="请输入手续费值"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div style="margin-left: 70px">
      <el-button type="primary" @click="fancyNumberSubmit()">保存</el-button>
    </div>
    <el-divider></el-divider>
    <div class="header">推广收益设置</div>
    <div v-for="(item,index) in promotionEarnings" :key="item.id">
      <el-form  label-width="160px" :inline="true" >
        <el-form-item :label="'V'+item.userLevel+'等级'+'直推数量'" >
          <el-input-number v-model="item.userDirectCount"  :min="1" :max="9999999" label="请输入直推数量"></el-input-number>
        </el-form-item>
        <el-form-item :label="'V'+item.userLevel+'等级'+'团队人数'" >
          <el-input-number v-model="item.userTeamCount"  :min="1" :max="9999999" label="请输入团队数量"></el-input-number>
        </el-form-item>
        <el-form-item :label="'V'+item.userLevel+'等级团队收益'+'比例'" >
          <el-input-number v-model="item.bilv"  :min="0.0001" :max="9999999" label="请输入比例"></el-input-number>
        </el-form-item>

      </el-form>
    </div>
    <div style="margin-left: 70px">
      <el-button type="primary" @click="promotionEarningsSubmit()">保存</el-button>
    </div>
  </div>
</template>
<script>
import {
  mGiftingSet,
  mGiftingSetUpdate,
  mLoginSet,
  mLoginSetUpdate,
  mProductSet,
  mProductSetUpdate,
  mBenefitConfig,
  mBenefitConfigUpdate,
  boxEnergyList,
  boxStatisticalList,
  boxEnergyUpdate,
  boxStatisticalUpdate,
  fancyConfigList,
  fancyConfigUpdate,
  userlevelConfig,
  userlevelConfigUpdate
} from "@/api/settings/settings";

export default {
  data() {
    return {
      // 推广
      promotionEarnings:[],
      // 靓号
      fancyNumber:[],
      transferRules: {
        endnumber: [
          {required: true, message: '请输入上限数量', trigger: 'blur'}
        ],
        endtime: [
          {required: true, message: '请选择结束时间', trigger: 'blur'}
        ],
        startnumber: [
          {required: true, message: '请输入起始数量', trigger: 'blur'}
        ],
        starttime: [
          {required: true, message: '请选择开始时间', trigger: 'blur'}
        ],
        sxf:[
          {required: true, message: '请输入手续费', trigger: 'blur'}
        ],

      },
      marketRules: {
        bilv: [
          {required: true, message: '请输入抽成比率', trigger: 'blur'}
        ],
        endAmout: [
          {required: true, message: '请输入上限数值', trigger: 'blur'}
        ],
        endNumber: [
          {required: true, message: '请输入上限数量', trigger: 'blur'}
        ],
        orderTime: [
          {required: true, message: '请输入撤单时效', trigger: 'blur'}
        ],
        startAmout: [
          {required: true, message: '请输入下限数值', trigger: 'blur'}
        ],
        startNumber: [
          {required: true, message: '请输入下限数量', trigger: 'blur'}
        ]
      },
      // 转赠
      transfer: {
        endnumber: undefined,
        endtime: undefined,
        startnumber: undefined,
        starttime: undefined,
        status: 0,
        sxf:undefined
      },
      // 登录
      login: {
        status: 0
      },
      // 市场
      market: {
        bilv: undefined,
        endAmout: undefined,
        endNumber: undefined,
        orderTime: undefined,
        startAmout: undefined,
        startNumber: undefined,
        type: 0
      },
      //分成
      divideInto:[],
      EnergyList:[],
      StatisticalList:[]
    }
  },
  created() {
    this.getmGiftingSet()
    this.getmLoginSet()
    this.getmProductSet()
    this.getmBenefitConfig()
    this.getboxEnergyList()
    this.getboxStatisticalList()
    this.getPromotionEarnings()
    this.getFancyNumber()
  },
  methods: {
    // 推广获取
    async getPromotionEarnings(){
      let res = await userlevelConfig()
      this.promotionEarnings = res.data
    },
    // 推广保存
    async promotionEarningsSubmit(){
      await userlevelConfigUpdate({list:this.promotionEarnings})
      this.$message({
        message: '修改成功',
        type: 'success'
      });
    },
    // 靓号获取
    async getFancyNumber(){
      let res = await fancyConfigList()
      this.fancyNumber = res.data
    },
    // 靓号保存
    async fancyNumberSubmit(){
      let isRun = true
      this.fancyNumber.forEach(item=>{
        if(!item.bilv){
          isRun=false
          this.$message({
            message: '请输入手续费',
            type: 'warning'
          });
          return
        }
      })

      if(isRun){
        await fancyConfigUpdate({list:this.fancyNumber})
        this.$message({
          message: '修改成功',
          type: 'success'
        });
      }
    },
    async StatisticalListoSubmit(){
      let ids = this.StatisticalList.map(item=>{
        return{
          id:item.id,
          prob:item.prob
        }
      })
      await boxStatisticalUpdate({list:ids})
      this.$message({
        message: '修改成功',
        type: 'success'
      });
    },
    async getboxStatisticalList(){
      let res = await boxStatisticalList()
      let list =res.data
      this.StatisticalList = list.list
    },
    async EnergyListSubmit(){
      await boxEnergyUpdate({list:this.EnergyList})
      this.$message({
        message: '修改成功',
        type: 'success'
      });
    },
    async getboxEnergyList(){
      let res = await boxEnergyList()
      this.EnergyList = res.data.list
    },
    // 分成更新
    async divideIntoSubmit(item){
      if(item.bilv==''){
        this.$message({
          message: '分成比率不能为空',
          type: 'warning'
        });
        return
      }
      let list = []
      list.push(item)
      await mBenefitConfigUpdate({list: list})
      this.$message({
        message: '修改成功',
        type: 'success'
      });
    },
    // 转赠
    transferSubmit() {
      this.$refs['transfer'].validate(async valid => {
        await mGiftingSetUpdate(this.transfer)
        this.$message({
          message: '修改成功',
          type: 'success'
        });
      })
    },
    // 登录
    async loginSubmit() {
      await mLoginSetUpdate(this.login)
      this.$message({
        message: '修改成功',
        type: 'success'
      });
    },
    onSubmit() {
      this.$refs['market'].validate(async valid => {
        await mProductSetUpdate(this.market)
        this.$message({
          message: '修改成功',
          type: 'success'
        });
      })
    },
    async getmBenefitConfig(){
      let res = await mBenefitConfig()
      this.divideInto = res.data.list
    },
    async getmGiftingSet() {
      let res = await mGiftingSet()
      this.transfer = res.data
    },
    async getmLoginSet() {
      let res = await mLoginSet()
      this.login = res.data
    },
    async getmProductSet() {
      let res = await mProductSet()
      this.market = res.data
    }
  }
}
</script>
<style scoped lang="scss" scoped>
.header {
  margin-left: 10px;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 10px;
}
</style>
